package com.oig.sys.base.service.impl;


import com.oig.sys.base.dao.ITbHsTariffAgreementDao;
import com.oig.sys.base.dto.HsTariffAgreementDto;
import com.oig.sys.base.entity.TbHsTariffAgreement;
import com.oig.sys.base.service.ITbHsTariffAgreementService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbHsTariffAgreementServiceImpl implements ITbHsTariffAgreementService {

    private final ITbHsTariffAgreementDao hsTariffAgreementDao;

    @Transactional
    @Override
    public void addOrUpdate(HsTariffAgreementDto hsTariffAgreementDto) {
        TbHsTariffAgreement hsTariffAgreement = hsTariffAgreementDao.findByHsCodeAndAgreementCode(hsTariffAgreementDto.getHsCode(), hsTariffAgreementDto.getAgreementCode());
        if (hsTariffAgreement==null){
            hsTariffAgreement = new TbHsTariffAgreement();
        }
        BeanUtils.copyProperties(hsTariffAgreementDto, hsTariffAgreement,"id");
        this.hsTariffAgreementDao.save(hsTariffAgreement);
    }

    @Transactional
    @Override
    public void addOrUpdateList(List<HsTariffAgreementDto> hsTariffAgreementDtoList) {
        log.debug("add or update {}", hsTariffAgreementDtoList);
        hsTariffAgreementDtoList.forEach(this::addOrUpdate);
    }
}
